package org.apache.servicecomb.samples.practise.houserush.user.center.service;



import org.apache.http.HttpStatus;
import org.apache.servicecomb.samples.practise.houserush.user.center.aggregate.Building;
import org.apache.servicecomb.samples.practise.houserush.user.center.aggregate.House;
import org.apache.servicecomb.samples.practise.houserush.user.center.aggregate.Realestate;
import org.apache.servicecomb.samples.practise.houserush.user.center.dao.BuildingDao;
import org.apache.servicecomb.samples.practise.houserush.user.center.dao.HouseDao;
import org.apache.servicecomb.samples.practise.houserush.user.center.dao.RealestateDao;
import org.apache.servicecomb.swagger.invocation.exception.InvocationException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DataRetrievalFailureException;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @Author: Snake Man
 * @system: 业务层
 * @DateTime: 2021/12/28 18:34
 * @Description:
 */
@Service
public class RealestateServiceImpl implements RealestateService {

    @Autowired
    private RealestateDao realestateDao;


    /**
     * 新增楼盘
     *
     * @param realestate
     * @return
     */
    public Realestate createRealesate(Realestate realestate) {
        return realestateDao.save(realestate);
    }

    /**
     * 查询楼盘
     *
     * @param id
     * @return
     */
    public Realestate findRealestate(Integer id) {
        return realestateDao.findOne(id);
    }

    /**
     * 查询所有楼盘
     *
     * @return
     */
    public List<Realestate> indexRealestates() {
        return realestateDao.findAll();
    }

    /**
     * 修改楼盘信息
     *
     * @param realestate
     * @return
     */
    public Realestate updateRealestate(Realestate realestate) {
        int id = realestate.getId();
        if (realestateDao.exists(id)) {
            return realestateDao.save(realestate);
        } else {
            throw new DataRetrievalFailureException("数据不存在，无法修改");
        }
    }

    /**
     * 删除楼盘
     *
     * @param id
     */
    public void removeRealestate(int id) {
        realestateDao.delete(id);
    }


    @Autowired
    private BuildingDao buildingDao;


    /**
     * 新增建筑楼
     *
     * @param realestateId
     * @param building
     * @return
     */
    public Building createBuilding(int realestateId, Building building) {
        Realestate realestate = realestateDao.findOne(realestateId);
        if (null != realestate) {
            building.setRealestate(realestate);
            return buildingDao.save(building);
        } else {
            throw new DataRetrievalFailureException("楼盘不存在，无法添加");
        }
    }

    /**
     * 查询建筑楼
     *
     * @param id
     * @return
     */
    public Building findBuilding(Integer id) {
        return buildingDao.findOne(id);
    }

    /**
     * 修改建筑楼
     *
     * @param building
     * @return
     */
    @Transactional
    public Building updateBuilding(Building building) {
        int id = building.getId();
        if (buildingDao.exists(id)) {
            return buildingDao.save(building);
        } else {
            throw new DataRetrievalFailureException("楼栋不存在，无法修改");
        }
    }

    /**
     * 删除建筑楼
     *
     * @param id
     */
    @Transactional
    public void removeBuilding(Integer id) {
        buildingDao.delete(id);
    }


    /**
     * 查询楼盘下所有建筑楼
     *
     * @param realestateId
     * @return
     */
    public List<Building> indexBuildings(Integer realestateId) {
        Realestate realestate = realestateDao.findOne(realestateId);
        return realestate.getBuildings();
    }

    @Autowired
    private HouseDao houseDao;

    /**
     * 新增房源
     *
     * @param buildingId
     * @param house
     * @return
     */
    @Transactional
    public House createHouse(Integer buildingId, House house) {
        Building building = buildingDao.findOne(buildingId);
        if (null != building) {
            house.setBuilding(building);
            House save = houseDao.save(house);
            return save;
        } else {
            throw new DataRetrievalFailureException("建筑类不存在，无法添加");
        }
    }

    /**
     * 查询房源信息
     *
     * @param id
     * @return
     */
    public House findHouse(Integer id) {
        return houseDao.findOne(id);
    }

    /**
     * 查询建筑类的所有房源
     *
     * @param buildingId
     * @return
     */
    public List<House> indexHouses(Integer buildingId) {
        Building building = buildingDao.findOne(buildingId);
        if (building != null) {
            return building.getHouses();
        } else {
            throw new DataRetrievalFailureException("建筑楼不存在");
        }
    }

    /**
     * 修改房源
     *
     * @param house
     * @return
     */
    public House updateHouse(House house) {
        int id = house.getId();
        if (houseDao.exists(id)) {
            return houseDao.save(house);
        } else {
            throw new DataRetrievalFailureException("房源不存在，无法修改");
        }
    }

    /**
     * 删除房源
     *
     * @param id
     */
    public void removeHouse(Integer id) {
        House house = houseDao.findOne(id);
        if (house.getState().equals("locking")) {
            throw new DataRetrievalFailureException("房源以开售，无法删除");
        }
        houseDao.delete(id);
    }

    /**
     * 锁定房源
     * @param ids
     * @return
     */
    @Transactional
    @Override
    public List<House> lockHousesForSale(List<Integer> ids) {
        //根据房源的id批量查询房源
        List<House> houses = houseDao.findAllByIdForUpdate(ids);

        //遍历房源
        houses.forEach(house -> {
            //判断房源是否已经被锁定
            if(!"new".equals(house.getState())){
                //如果被锁定直接抛出异常
                throw new InvocationException(HttpStatus.SC_FORBIDDEN,"","房源"+house.getId()+"已被锁定");
            }
        });
        //根据房源的id批量修改房源，把房源的状态修改为锁定状态
        houseDao.updateLockingStatesForUpdate(ids);
        return houses;
    }

    /**
     * 解锁房源
     * @param ids
     * @return
     */
    @Transactional
    public int updateReleaseLockingStattesForHouses(List<Integer> ids) {
        return houseDao.updateReleaseLockingStattesForHouses(ids);
    }
}
